package itpenguin.review;

import itpenguin.review.entities.Item;
import itpenguin.review.util.ReviewUtil;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReviewServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String itemId = null;
		try {
			itemId = ReviewUtil.getItemId(req);
		} catch (ServletException e) {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		}
		
		Item item = ReviewUtil.getItem(itemId);
		if (item == null) {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND);
			return ;
		}
		
		req.setAttribute("item", item);
		
		resp.addDateHeader("Last-Modified", item.getLastModified().getTime());
		
		RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/template/main.jsp");
		rd.forward(req, resp);
	}
}
